package com.zzsy.shoppingcardemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzsy.shoppingcardemo.MainActivity;
import com.zzsy.shoppingcardemo.R;
import com.zzsy.shoppingcardemo.bean.GoodsBean;
import com.zzsy.shoppingcardemo.view.AddSubView;

import java.util.List;

/**
 * Created by Administrator on 2019/3/27 0027.
 */

public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder> {

    private Context context;
    private List<GoodsBean> mAllData;
    private TextView mTvShopcartTotal;
    private CheckBox mCheckboxAll;
    /**
     * 完成状态下的删除
     */
    private CheckBox mCbAll;


    public ShoppingCartAdapter(Context mContext, List<GoodsBean> allData, TextView tvShopcartTotal, CheckBox checkboxAll, CheckBox cbAll) {

        this.context = mContext;
        this.mAllData = allData;
        this.mTvShopcartTotal = tvShopcartTotal;
        this.mCheckboxAll = checkboxAll;
        this.mCbAll = cbAll;
        /**
         * 总金额
         * */
        ShowTotalPrice();
        /**
         * 设置点击事件
         * */
        setListener();
        /**
         * 校验是否全选
         * */
        checkAll();

    }

    /**
     * 设置点击事件
     */
    private void setListener() {

        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int positon) {
                //根据位置得到对应的bean
                GoodsBean goodsBean = mAllData.get(positon);
                //设置取反装填
                goodsBean.setSelected(!goodsBean.isSelected());
                //刷新数据
                notifyItemChanged(positon);
                //校验是否是全选
                checkAll();
                //重新计算
                ShowTotalPrice();
            }
        });
        //设置全选和非全选按钮的点击事件
        mCheckboxAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到状态
                boolean isChecked = mCheckboxAll.isChecked();
                //根据状态设置全选和非全选
                checkAll_none(isChecked);
                //计算总价格
                ShowTotalPrice();
            }
        });

        mCbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到状态
                boolean isChecked = mCbAll.isChecked();
                //根据状态设置全选非全选
                checkAll_none(isChecked);
            }
        });
    }

    /**
     * 设置全选和非全选
     */
    public void checkAll_none(boolean isChecked) {

        if (mAllData != null && mAllData.size() >= 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                GoodsBean goodsBean = mAllData.get(i);
                goodsBean.setSelected(isChecked);
                notifyItemChanged(i);
            }
        }

    }

    /**
     * 校验是否全选
     */
    public void checkAll() {
        if (mAllData != null && mAllData.size() >= 0) {
            int number = 0;
            for (int i = 0; i < mAllData.size(); i++) {
                GoodsBean goodsBean = mAllData.get(i);
                if (!goodsBean.isSelected()) {
                    //非全选
                    mCheckboxAll.setChecked(false);
                    mCbAll.setChecked(false);
                } else {
                    number++;
                }
            }
            if (number == mAllData.size()) {
                //全选
                mCheckboxAll.setChecked(true);
                mCbAll.setChecked(true);
            }

        } else {
            mCheckboxAll.setChecked(false);
            mCbAll.setChecked(false);
        }
    }

    /**
     * 合计金额
     */
    public void ShowTotalPrice() {
        mTvShopcartTotal.setText("合计：" + getTotalPrices());
    }

    private double getTotalPrices() {

        double totalPrice = 0.0;
        if (mAllData != null && mAllData.size() >= 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                GoodsBean goodsBean = mAllData.get(i);
                if (goodsBean.isSelected()) {
                    totalPrice = totalPrice + Double.valueOf(goodsBean.getNumber()) * Double.valueOf(goodsBean.getCover_price());
                }
            }
        }
        return totalPrice;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.item_shop_cart, null);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //根据位置得到对应的bean
        final GoodsBean goodsBean = mAllData.get(position);
        //设置数据
        holder.cb_gov.setChecked(goodsBean.isSelected());
      Glide.with(context).load(goodsBean.getFigure()).into(holder.iv_gov);
        holder.tv_desc_gov.setText(goodsBean.getName());
        holder.tv_price_gov.setText("￥" + goodsBean.getCover_price());
        holder.add_sub_view.setValue(goodsBean.getNumber());
        holder.add_sub_view.setMinValue(1);
        holder.add_sub_view.setMaxValue(100);
        /**
         * 设置商品数量的变化
         * */
        holder.add_sub_view.setOnNumberChangerListener(new AddSubView.OnNumberChangerListener() {
            @Override
            public void onNumberChange(int value) {
                //列表更新
                goodsBean.setNumber(value);
                //刷新适配器
                notifyItemChanged(position);
                //再次计算总价格
                ShowTotalPrice();
            }
        });
    }
    public void deleteData() {

        if (mAllData != null && mAllData.size() > 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                //删除选中的
                GoodsBean goodsBean = mAllData.get(i);
                if (goodsBean.isSelected()) {
                    //列表移除
                    mAllData.remove(goodsBean);
                    //刷新
                    notifyItemChanged(i);
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return mAllData == null ? 0 : mAllData.size();
    }

    /**
     * 监听者
     */
    public interface OnItemClickListener {
        /**
         * 点击某一条目的时候暴露的接口方法
         */
        void onItemClick(int positon);

    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private CheckBox cb_gov;
        private ImageView iv_gov;
        private TextView tv_desc_gov;
        private TextView tv_price_gov;
        private AddSubView add_sub_view;


        public ViewHolder(View itemView) {
           super(itemView);
            cb_gov = itemView.findViewById(R.id.cb_gov);
            iv_gov = itemView.findViewById(R.id.iv_gov);
            tv_desc_gov = itemView.findViewById(R.id.tv_desc_gov);
            tv_price_gov = itemView.findViewById(R.id.tv_price_gov);
            add_sub_view = itemView.findViewById(R.id.add_sub_view);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(getLayoutPosition());
                    }

                }
            });
        }
    }
}

